const BASE_URL = 'https://mall-api.bjzhrs.shop/'
const assetsUrl = 'https://mall-api.bjzhrs.shop/'
const domainUrl = 'https://mall-api.bjzhrs.shop/'
let errorConfrim = false
class Request {
  constructor(url) {
    this.url = url
  }
  /**
   * 封装http请求
   * @param {string} url 
   * @param {string} methods 
   * @param {object} params
   * @param {object} header
   */
  request(url, methods, params, header, callback) {
		let token = wx.getStorageSync('access_token') || ''
		console.log(header)
    if (header) {
      header.Authorization = token
    } else {
      header = {
        "Authorization": token
      }
    }
    return new Promise((resolve, reject) => {
      let that = this
      wx.request({
        url: this.url + url,
        method: methods,
        data: params,
        header,
        success(res) {
          if (callback) {
            callback(res.data)
            return
          }
          if (res.data.code == 401||res.data.code==403) {
            // token失效
            that.reLogin().then(() => {
              header.Authorization = wx.getStorageSync('access_token')
              that.request(url, methods, params, header, resolve)
						})
						return;
					} 
					if(res.data.code==500){
						wx.showModal({
							title: '消息',
							content: res.data.msgres.data.msg,
							showCancel:false
						})
						return;
					}
					resolve(res.data);
					
					// else if (res.data.code == 201) {
            // 验证权限
            // wx.showToast({
            //   title: res.data.msg,
            //   icon: 'none',
            //   duration: 1500,
            //   success: () => {
            //     setTimeout(function () {
            //       // 清除本地信息
            //       wx.removeStorageSync('token')
            //       getApp().globalData.userInfo.account_type = ""
            //       getApp().globalData.userInfo.menu = ""
            //       getApp().globalData.userInfo.name = ""
            //       getApp().globalData.userInfo.token = ""
            //       // 关闭其他页面并跳转到首页
            //       wx.reLaunch({
            //         url: '/pages/index/index',
            //       })
            //     }, 1500)
            //   }
            // })
            // resolve()
          // } else if (res.data.code == 909 && methods == 'GET') {
            // wx.hideLoading()
            // if( !errorConfrim ){
            //   errorConfrim = true
            //   wx.showModal({
            //     title: '系统异常',
            //     content: '',
            //     showCancel: false,
            //     complete: (res) => {
            //       if (res.confirm) {
            //         errorConfrim = false
            //         wx.navigateBack()
            //         resolve()
            //       }
            //     }
            //   })
            // }
          // } else {
            // resolve(res.data)
          // }
        },
        fail(res){
          if( res.errMsg == "request:fail " ){
            wx.showModal({
              title: '错误',
              content: '网络请求超时，请稍后再试！',
              showCancel: false,
              complete: (res) => {
                // if (res.confirm) {
                //   wx.navigateBack()
                // }
                wx.hideLoading()
              }
            })
            reject(res)
          }
        }
      })
    })
  }
  get(url, params, header) {
    return this.request(url, 'GET', params, header)
  }
  post(url, params, header) {
    return this.request(url, 'POST', params, header)
  }
  postFrom(url, params, header) {
    return this.request(url, 'POST', params, header)
  }
  delete(url, params, header) {
    return this.request(url, 'DELETE', params, header)
  }
  put(url, params, header) {
    return this.request(url, 'PUT', params, header)
  }
  reLogin() {
    let that = this
    return new Promise((resolve, reject) => {
      wx.login({
        async success(res) {
          wx.request({
            url: that.url + `api/minip/v1/sessions`,
            method: 'POST',
            data: {
              code: res.code
            },
            success(resp) {
              if (resp.data.code == 0) {
                if (resp.data.data.token != '') {
                  wx.setStorageSync('token', resp.data.data.token.token)
                }
              }
              resolve()
            },
            fail(err) {
              reject(err)
            }
          })
        }
      })

    })
  }
}

export const request = new Request(BASE_URL)

export {
  BASE_URL,
  assetsUrl,
  domainUrl
}